# ----------------------------------------------------------------------------
#       Copyright (C) 2013-2014 Huynh Vi Lam  <domovilam@gmail.com>
#
#       This file is part of pimucha.
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------

import logging,sys,threading,time
if sys.hexversion > 0x030000F0:
    import queue as Queue
else:
    import Queue
QEmpty = Queue.Empty
from piHAlibs import (broadcast)
from .servers import SetupControllers

logger = logging.getLogger()


class MULTIEVTQ(object):
    def __init__(self):
        self.name = 'MULTIEVTQ'
        self.namedev = self.name + ':MEVTQ'
        self.setup = False
        self.opened = False
        self.stop = False
        self.featurenames = []
        self.features = []
        self.decodernames = []
        self.decoders = []
        self.dfeaturenames = []
        self.dfeatures = []
        self.evtouts = None
        self.multievtq = Queue.Queue()
        self.servercmds = {
            'STARTM':self.startm,
            'STOPS':self.stops}

    def tsetup(self):
        self.setup = True
        self.opened = True

    def cleanm(self):
        if not self.setup: return
        for d in self.decoders:
            d.close()
            self.decoders.remove(d)
        for d in self.decodernames:
            self.decodernames.remove(d)
        for f in self.features:
            f.close()
            self.features.remove(f)
        for f in self.featurenames:
            self.featurenames.remove(f)
        for f in self.dfeatures:
            f.close()
            self.dfeatures.remove(f)
        for f in self.dfeaturenames:
            self.dfeaturenames.remove(f)

    def dispatch(self,target):
        if not self.setup: return
        while (not self.stop) and (self.multievtq):
            event = None
            try:
                event = self.multievtq.get_nowait()
                target.send(event)
            except Queue.Empty:
                time.sleep(0.1)
        self.cleanm()

    def allRXTX(self):
        self.dispatch(
            target=broadcast(self.evtouts)
            )

    def startm(self):
        self.tt = threading.Thread(target=self.allRXTX)
        self.tt.name = 'MULTIEVTQOf_' + self.namedev
        self.tt.daemon = True
        self.tt.start()
        self.ttname = self.tt.name
        print('%s starting...' % self.tt.name)

    def stops(self):
        self.stop = True
        print('%s stopping...' % self.ttname)

